package cn.linran.org.controller;

import cn.linran.basic.util.ExcelUtils;
import cn.linran.basic.util.JsonResult;
import cn.linran.basic.util.PageList;
import cn.linran.basic.exception.BusinessException;
import cn.linran.org.domain.Shop;
import cn.linran.org.domain.ShopAuditLog;
import cn.linran.org.dto.ShopDto;
import cn.linran.org.query.ShopQuery;
import cn.linran.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/shop")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;


    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Shop findOne( @ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.save(shop);
            }else
                shopService.update(shop);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            return shopService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Shop>();//第一次查
        }
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            shopService.batchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 店铺入驻
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "商家入驻",notes = "")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlemnt(shop);
            return JsonResult.me();
        } catch (BusinessException e) {//业务异常捕获
            return JsonResult.me().setMsg("入驻失败"+e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            //代码问题（或者其它idea问题等等）
            return JsonResult.me().setMsg("系统异常，稍后重试");

        }
    }

    /**
     * 审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult pass(@RequestBody ShopAuditLog log){

        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }
    /**
     * 驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回" )
    public JsonResult reject(@RequestBody ShopAuditLog log){

        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核驳回");
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            // 查询所有shop数据
            List<Shop> list = shopService.loadAll();
            //调用工具了  (List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入商铺数据
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass

            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            // 打印结果
            list.forEach(System.out::println);
            //保存到数据库中[自己实现，提示：动态sql 批量插入]
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * domain，pojo.： 实体类----》跟数据库中的表对应的
     * query:  做查询参数使用
     * dto: 接收参数或者传参使用的
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }






}























